﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class NetWorkControl : NetworkManager
{
    #region ClientHandler
    IClientHandler loginRequestHandler;
    IClientHandler registerRequestHandler;
    #endregion

    #region ServerHandler
    IServerHandler loginResponse = new LoginResponseHanlder();
    IServerHandler registerResponse = new RegisterResponseHandler();
    #endregion
    public override void Start()
    {
        registerRequestHandler = new LoginRequestHandler();
        loginRequestHandler = new RegisterRequestHandler();
        //if (!NetworkServer.active && !NetworkClient.active)
        //{
        //    StartServer();
        //}
        //else if (NetworkServer.active)
        //{
        StartClient();
        //}
    }
  
    public override void OnStartServer()
    {
        base.OnStartServer();
        NetworkServer.RegisterHandler<NetMessage>(OnServerRecieve, false);

    }
    #region 服务端接收到消息
    private void OnServerRecieve(NetworkConnection conn, NetMessage message)
    {
        byte areaCode = message.AreaCode;

        switch (areaCode)
        {
            case AreaCode.Account:
                ProcessAccount(conn, message);
                break;
               
            default:
                break;
        }

    }

    /// <summary>
    /// 处理账号相关操作
    /// </summary>
    /// <param name="conn"></param>
    /// <param name="message"></param>
    void ProcessAccount(NetworkConnection conn, NetMessage message)
    {
        byte opCode = message.OperationCode;
        
        switch (opCode)
        {
            case OperationCode.Login:
                Debug.Log("打印账号"+  message.ParameterDic[0]);
                loginResponse.OnRecieveMsg(conn,message);
                break;

            case OperationCode.Register:
                registerResponse.OnRecieveMsg(conn,message);
                break;
            default:
                break;
        }
    }

    #endregion


    #region 客户端收到消息
    /// <summary>
    /// 注册事件的入口函数
    /// </summary>
    public override void OnStartClient()
    {
        base.OnStartClient();
        NetworkClient.RegisterHandler<NetMessage>(OnClientRecieved);
    }

    /// <summary>
    /// 客户端接收到消息的分发器
    /// </summary>
    /// <param name="msg"></param>
    private void OnClientRecieved(NetMessage msg)
    {
        byte areaCode = msg.AreaCode;
        switch (areaCode)
        {
            case AreaCode.Account:

                break;

            default:
                break;
        }
    } 
    #endregion

    public override void OnServerConnect(NetworkConnection conn)
    {
        base.OnServerConnect(conn);
    }
    public override void OnClientConnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);

        Debug.Log("打印客户端已连接" + conn.address);
    }
   
    public override void OnClientDisconnect(NetworkConnection conn)
    {
        base.OnClientDisconnect(conn);
        Debug.Log("打印客户端已断开" + conn.address);

    }

}
